﻿using System;
using System.Web.UI.WebControls;

using System.Text;

namespace IFD2
{
    public partial class NewOpenOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("SA3") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
            }

            txtOri1.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtOri1',event)");
            txtDes1.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtDes1',event)");
            txtOri2.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtOri2',event)");
            txtDes2.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtDes2',event)");
            txtOri3.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtOri3',event)");
            txtDes3.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtDes3',event)");
            txtOri4.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtOri4',event)");
            txtDes4.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtDes4',event)");

            txtOri1.Attributes.Add("onkeydown", "key_down(event)");
            txtDes1.Attributes.Add("onkeydown", "key_down(event)");
            txtOri2.Attributes.Add("onkeydown", "key_down(event)");
            txtDes2.Attributes.Add("onkeydown", "key_down(event)");
            txtOri3.Attributes.Add("onkeydown", "key_down(event)");
            txtDes3.Attributes.Add("onkeydown", "key_down(event)");
            txtOri4.Attributes.Add("onkeydown", "key_down(event)");
            txtDes4.Attributes.Add("onkeydown", "key_down(event)");

            txtOri1.Attributes.Add("autocomplete", "off");
            txtDes1.Attributes.Add("autocomplete", "off");
            txtOri2.Attributes.Add("autocomplete", "off");
            txtDes2.Attributes.Add("autocomplete", "off");
            txtOri3.Attributes.Add("autocomplete", "off");
            txtDes3.Attributes.Add("autocomplete", "off");
            txtOri4.Attributes.Add("autocomplete", "off");
            txtDes4.Attributes.Add("autocomplete", "off");

            lblError.Text = "";
            form1.DefaultButton = btnQueryFares.ID;
        }

        protected void dlTripCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dlTripCount.Text == "1")
            {
                txtOri1.Visible = true;
                txtDes1.Visible = true;
                txtOri2.Visible = false;
                txtDes2.Visible = false;
                txtOri3.Visible = false;
                txtDes3.Visible = false;
                txtOri4.Visible = false;
                txtDes4.Visible = false;

            }
            else if (dlTripCount.Text == "2")
            {
                txtOri1.Visible = true;
                txtDes1.Visible = true;
                txtOri2.Visible = true;
                txtDes2.Visible = true;
                txtOri3.Visible = false;
                txtDes3.Visible = false;
                txtOri4.Visible = false;
                txtDes4.Visible = false;

            }
            else if (dlTripCount.Text == "3")
            {
                txtOri1.Visible = true;
                txtDes1.Visible = true;
                txtOri2.Visible = true;
                txtDes2.Visible = true;
                txtOri3.Visible = true;
                txtDes3.Visible = true;
                txtOri4.Visible = false;
                txtDes4.Visible = false;

            }
            else if (dlTripCount.Text == "4")
            {
                txtOri1.Visible = true;
                txtDes1.Visible = true;
                txtOri2.Visible = true;
                txtDes2.Visible = true;
                txtOri3.Visible = true;
                txtDes3.Visible = true;
                txtOri4.Visible = true;
                txtDes4.Visible = true;

            }
        }

        protected void save_session()
        {
            int trip_count, adt, chd, inf;
            TextBox[] txt_ori = new TextBox[4];
            txt_ori[0] = txtOri1;
            txt_ori[1] = txtOri2;
            txt_ori[2] = txtOri3;
            txt_ori[3] = txtOri4;
            TextBox[] txt_des = new TextBox[4];
            txt_des[0] = txtDes1;
            txt_des[1] = txtDes2;
            txt_des[2] = txtDes3;
            txt_des[3] = txtDes4;
            //TextBox[] txt_date = new TextBox[4];

            trip_count = Convert.ToInt16(dlTripCount.SelectedValue);
            Session["TRIP_COUNT"] = trip_count;

            adt = Convert.ToInt16(txtAdtCount.Text);
            chd = Convert.ToInt16(txtChdCount.Text);
            inf = Convert.ToInt16(txtInfCount.Text);
            if (adt < 0 || chd < 0 || inf < 0 || (adt < inf) || adt + chd > 50)
            {
                lblError.Text = "Illegal Passenger Count!";
                return;
            }
            Session["ADT_COUNT"] = adt;
            Session["CHD_COUNT"] = chd;
            Session["INF_COUNT"] = inf;
            Session["SEATS"] = adt + chd;

            StringBuilder buf = new StringBuilder();
            buf.Clear();
            //DateTime last_date = Convert.ToDateTime("1999-12-31");
            //DateTime curr_date;
            for (int i = 1; i <= trip_count; i++)
            {
                //try
                //{
                //    DateTime.Parse(txt_date[i - 1].Text);
                //}
                //catch
                //{
                //    lblError.Text = "Invalid date format!";
                //    return;
                //}
                //curr_date = Convert.ToDateTime(txt_date[i - 1].Text);
                //if (last_date > curr_date)
                //{
                //    lblError.Text = "Flight dates are disorder!";
                //    return;
                //}
                Session["ORI_" + i.ToString()] = txt_ori[i - 1].Text.ToUpper();
                Session["DES_" + i.ToString()] = txt_des[i - 1].Text.ToUpper();
                //Session["FLT_DATE_" + i.ToString()] = curr_date.ToLongDateString();

                //last_date = curr_date;
            }
        }

        protected void butFareQuery_Click(object sender, EventArgs e)
        {
            save_session();
            Response.Redirect("AvailOpenFares.aspx");
        }
    }
}